package hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * @author: kevin
 * @date: 13:53 2021/5/2
 */
@SuppressWarnings("all")
public class HdfsTest {
    private FileSystem fileSystem;
    @Before
    public void before() throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","hdfs://master:9000");
        fileSystem = FileSystem.get(conf);
    }

    @After
    public void after() throws IOException {
        fileSystem.close();
    }

    @Test
    public void listFiles() throws IOException {
        RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/"),true);
        while(files.hasNext()){
            LocatedFileStatus next = files.next();
            System.out.println(next.getPath());
        }
    }

    //读取指定文件
    @Test
    public void readFile() throws IOException {
        String remotePath = "/user/hadoop/";
        String fileName = "hello.txt";
        FSDataInputStream open = fileSystem.open(new Path(remotePath + fileName));
        byte[] buf = new byte[1024];
        int len;
        while((len = open.read(buf)) != -1){
            System.out.println(new String(buf,0,len));
        }
        open.close();
    }

    //创建文件
    @Test
    public void createFile() throws IOException {
        String remoteFile = "/user/hadoop/file5.abc";
        FSDataOutputStream out = fileSystem.create(new Path(remoteFile));
        out.write("this is file5.abc".getBytes());
        System.out.println("创建成功");
        out.flush();
        out.close();
    }

    //下载文件
    @Test
    public void downloadFile() throws IOException {
        String remoteFile = "/user/hadoop/file5.abc";
        String localFile = "data/download.txt";
        fileSystem.copyToLocalFile(new Path(remoteFile),new Path(localFile));
    }

    //上传文件
    @Test
    public void uploadFile() throws IOException {
        String remoteFile = "/user/hadoop/upload.txt";
        String localFile = "data/test.txt";
        fileSystem.copyFromLocalFile(new Path(localFile),new Path(remoteFile));
    }

    //删除文件
    @Test
    public void deleteFile() throws IOException {
        String remoteFile = "/user/hadoop/file5.abc";
        boolean delete = fileSystem.delete(new Path(remoteFile),true);
        if (delete)
            System.out.println("删除成功");
        else
            System.out.println("删除失败");
    }

}
